package mybatis01;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.etc.dao.BlogMapper;
import com.etc.entity.Blog;

public class TestMyBatis01 {

	public static void main(String[] args) throws IOException {
		// 1- 创建resource获取mybatis的配置文件
		String resource = "mybatis-config.xml";
		// 1-1 通过文件流读取mybatis的全局配置文件
		InputStream inputStream = Resources.getResourceAsStream(resource);
		// 2- sqlSessionFactory对象 ,工厂的作用获取SqlSessio
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		// 3. SqlSession 得到SqlSession对象
		SqlSession session = sqlSessionFactory.openSession();
		// 得到maper对象->session.getMapper->mapperRegistry->
		// mapperProxyFactory.newInstance(sqlSession)-> Proxy.newProxyInstance
		BlogMapper mapper = session.getMapper(BlogMapper.class);
		// get方法得到Blog对象
		Blog blog = mapper.selectByPrimaryKey(1);
		// 输出结果
		System.out.println("blog :" + blog.getId() + "," + blog.getBlogtitle() + "," + blog.getContent() + ","
				+ blog.getAuthorid());
		session.close();
		inputStream.close();

	}

}
